﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using GradeManager.ServiceReference1;

namespace GradeManager.classes
{
    public class Manager : Person
    {
        public Manager(string ID, string password)
        {
            this.ID = ID;
            this.password = password;
        }
        int cmp(Score a, Score b)
        {
            if (a.grade > b.grade)
            {
                return 1;
            }
            return 0;
        }
        public bool sortGrade(string courseID)
        {
            List<Score> tmpList = new List<Score>();
            for (int i = 0; i < MainPage.scoreList.Count; i++)
            {
                if (MainPage.scoreList[i].courseID == courseID)
                {
                    tmpList.Add(MainPage.scoreList[i]);
                }
            }

            if (tmpList.Count == 0)
            {
                return false;
            }

            tmpList.Sort(cmp);

            string content = "";
            for (int i = 0; i < tmpList.Count; i++)
            {
                content += tmpList[i].studentID + " ";
                content += tmpList[i].grade.ToString() + " ";
            }

            Service1Client client = new Service1Client();
            client.StoreCourseGradeAsync(courseID, content);

            return true;
        }
        public int selectGood()
        {
            string content = "";
            int num = 0;
            //遍历所有学生
            for (int i = 0; i < MainPage.allStudents.Count; i++)
            {
                bool flag = true;
                for (int j = 0; j < MainPage.scoreList.Count; j++)
                {
                    if (MainPage.scoreList[j].studentID == MainPage.allStudents[i].ID && MainPage.scoreList[j].grade < 85)
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                {
                    content += MainPage.allStudents[i].ID + "\n";
                    num++;
                }
            }

            if (num > 0)
            {
                Service1Client client = new Service1Client();
                client.StoreGoodStuAsync(content);
            }
            return num;
        }
        public bool statistics()
        {
            string content = "";
            for (int j = 0; j < MainPage.allCourses.Count; j++)
            {
                int stuNum = 0;
                int failNum = 0;
                int maxGrade = int.MinValue;
                int minGrade = int.MaxValue;
                float sumGrade = 0.0f;

                for (int i = 0; i < MainPage.scoreList.Count; i++)
                {
                    if (MainPage.scoreList[i].courseID == MainPage.allCourses[j].ID)
                    {
                        if (MainPage.scoreList[i].grade < 60)
                        {
                            failNum++;
                        }
                        if (MainPage.scoreList[i].grade > maxGrade)
                        {
                            maxGrade = MainPage.scoreList[i].grade;
                        }
                        if (MainPage.scoreList[i].grade < minGrade)
                        {
                            minGrade = MainPage.scoreList[i].grade;
                        }
                        sumGrade += MainPage.scoreList[i].grade;
                        stuNum++;
                    }
                }
                if (sumGrade == 0.0f)
                {
                    return false;
                }

                sumGrade /= stuNum;
                content += MainPage.allCourses[j].ID + " ";
                content += failNum.ToString() + " ";
                content += maxGrade.ToString() + " ";
                content += minGrade.ToString() + " ";
                content += sumGrade.ToString() + " ";
            }

            Service1Client client = new Service1Client();
            client.CourseStatisticsAsync(content);
            return true;
        }
    }
}
